package com.iwith.dongmoon.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.util.EncryptionManager;
import com.iwith.dongmoon.util.RequestData;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;

public class SignUpInfoActivity extends Activity implements OnClickListener, OnTouchListener{
	private LinearLayout rlayout;
	private EditText id;
	private EditText name;
	private EditText nickname;
	private EditText graduateClass;
	private EditText password;
	private EditText email;
	private Button nextBt;
	private Button prevBt;
	private ImageView title;
	
	private Context mContext;
	
	private BoardList boardList;
	private RequestGson requestGson=new RequestGson();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup_info);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		Initialize();
	}
	
	private void Initialize(){
		mContext = this;
		
		title = (ImageView) findViewById(R.id.title);
		rlayout = (LinearLayout) findViewById(R.id.signup_info_relativelayout1);
		id = (EditText) rlayout.findViewById(R.id.signup_info_id);
		name = (EditText) rlayout.findViewById(R.id.signup_info_name);
		nickname = (EditText) rlayout.findViewById(R.id.signup_info_nickname);
		graduateClass = (EditText) rlayout.findViewById(R.id.signup_info_class);
		email = (EditText) rlayout.findViewById(R.id.signup_info_email);
		password = (EditText) rlayout.findViewById(R.id.signup_info_password);
		nextBt = (Button) rlayout.findViewById(R.id.signup_info_nextbt);
		prevBt = (Button) findViewById(R.id.backbtn);

		PasswordTransformationMethod PassWtm = new PasswordTransformationMethod();
		password.setTransformationMethod(PassWtm);

		
		rlayout.setOnTouchListener(this);
		nextBt.setOnClickListener(this);
		prevBt.setOnClickListener(this);
		title.setOnClickListener(this);
	}

	public boolean onTouch(View v, MotionEvent arg1) {
		if(v.getId()==rlayout.getId()){
			InputMethodManager padControl;
			padControl = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			padControl.hideSoftInputFromWindow(rlayout.getWindowToken(), 0);

			return true;
		}
		return false;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==nextBt.getId()){
			// TODO NOTE : id check part, duplication check is needed
			if (id.getText().toString().length() != 0) {
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, "/checkID", "GET", "?userid=" + id.getText().toString());

				// TODO NOTE : id check, when user id is duplicated
				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == false) {
					requestGson.alertToast(mContext, "사용중인 아이디");
					return;
				}
			} else { // TODO NOTE : id field is empty
				requestGson.alertToast(mContext, "아이디를 입력해 주세요");
				return;
			}
			// TODO NOTE : name check part, no duplication check
			if (name.getText().toString().length() == 0) {
				requestGson.alertToast(mContext, "이름을 입력해 주세요");
				return;
			}

			// TODO NOTE : nickname check part, duplication check is needed
			if (nickname.getText().toString().length() != 0) {
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, "/checkNickname","GET",	"?nickname=" + nickname.getText().toString());

				// TODO NOTE : id check, when user nickname is duplicated
				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == false) {
					requestGson.alertToast(mContext, "사용중인 별명");
					return;
				}
			} else { // TODO NOTE : nickname field is empty
				requestGson.alertToast(mContext, "별명을 입력해 주세요");
				return;
			}

			// TODO NOTE : graduate class check part, no duplication check
			if (graduateClass.getText().toString().length() == 0) {
				requestGson.alertToast(mContext, "기수를 입력해 주세요");
				return;
			}

			// TODO NOTE : email check part, duplication check is needed
			if (email.getText().toString().length() != 0) {
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, "/checkEmail", "GET", "?email=" + email.getText().toString());

				// TODO NOTE : email check, when email is duplicated
				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == false) {
					requestGson.alertToast(mContext, "사용중인 이메일");
					return;
				}
			} else { // TODO NOTE : when email field is empty
				requestGson.alertToast(mContext, "이메일을 입력해 주세요");
				return;
			}

			// TODO NOTE : password check part, no duplication check
			if (password.getText().toString().length() == 0) {
				requestGson.alertToast(mContext, "비밀번호를 입력해 주세요");
				return;
			}

			Gson gson = new Gson();
			RequestData data = new RequestData(mContext, "/registerUser", "", "");
			data.addParameter("userid", id.getText().toString());
			data.addParameter("password", EncryptionManager.SHA1(password.getText().toString()));
			data.addParameter("username", name.getText().toString());
			data.addParameter("nickname", nickname.getText().toString());
			data.addParameter("email", email.getText().toString());
			data.addParameter("graduation", graduateClass.getText().toString());
			
			String sresult = data.getDataPost();
			boardList = gson.fromJson(sresult, BoardList.class);
			if (boardList.getResultCode().equals(ResultCode.SUCCESS) == false) {
				requestGson.alertToast(mContext, "가입 실패");
				return;
			} else {
				Intent intent = new Intent();
				intent.putExtra("userid", id.getText().toString());
				intent.putExtra("password", password.getText().toString());
				setResult(RESULT_OK, intent);
				finish();
			}
		} else if(v.getId()==prevBt.getId()){
			finish();
		} else if(v.getId()==title.getId()){
			Intent intent=new Intent(this, MainIconView.class);
			intent.putExtra("flag", true);
			startActivity(intent);
		}
	}
}
